package com.ztsoft.user.service.project.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztsoft.user.mapper.project.SecondBargainMapper;
import com.ztsoft.user.pojo.entity.project.Choose;
import com.ztsoft.user.pojo.entity.project.Project;
import com.ztsoft.user.pojo.entity.project.SecondBargain;
import com.ztsoft.user.pojo.entity.user.SysUser;
import com.ztsoft.user.pojo.vo.PageData;
import com.ztsoft.user.pojo.vo.PageInfo;
import com.ztsoft.user.service.project.ChooseService;
import com.ztsoft.user.service.project.ProjectService;
import com.ztsoft.user.service.project.SecondBargainService;
import com.ztsoft.user.service.user.SysUserService;
import com.ztsoft.user.utils.ChargeCalculateUtils;
import com.ztsoft.user.utils.Result;
import com.ztsoft.user.utils.UserHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class SecondBargainImpl extends ServiceImpl<SecondBargainMapper, SecondBargain> implements SecondBargainService {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private ChooseService chooseService;
    @Resource
    private ProjectService projectService;


    @Override
    public Result<Object> updateSecondBargain(SecondBargain secondBargain) {
        if (updateById(secondBargain)) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> addSecondBargain(SecondBargain secondBargain) {
        if(count(new QueryWrapper<SecondBargain>().eq("order_id", secondBargain.getOrderId())) >= 1) {
            return Result.returnMsg("不能重复发起二次议价", 1001);
        }

        Choose choose = chooseService.getById(secondBargain.getOrderId());

        if(choose.getPrice() < Double.parseDouble(secondBargain.getAmount())) {
            return Result.returnMsg("二次议价金额不能大于分包金额", 1001);
        }

        UserHolder userInfo = sysUserService.getUserInfo();
        SysUser sysUser = userInfo.getSysUser();
        secondBargain.setPerson(sysUser.getUserName());
        secondBargain.setPerson(sysUser.getUserName());
        secondBargain.setStatus("0");
        secondBargain.setBargainTime(new Date());
        save(secondBargain);
        return Result.success(secondBargain);
    }

    @Override
    public Result<Object> deleteSecondBargain(String id) {
        if (removeById(id)) {
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Object> querySecondBargain(String projectId, String status, Integer currentPage, Integer pageSize) {
        QueryWrapper<SecondBargain> secondBargainQueryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(projectId)) {
            secondBargainQueryWrapper.eq("project_id", projectId);
        }

        if (ObjectUtil.isNotNull(status) && !"0".equals(status)) {
            if(status.contains(",")) {
                secondBargainQueryWrapper.in("status", status.split(","));
            } else {
                secondBargainQueryWrapper.eq("status", status);
            }
        }

        Page<SecondBargain> page = new Page<>(currentPage, pageSize);
        Page<SecondBargain> secondBargainPage = page(page, secondBargainQueryWrapper);
        List<SecondBargain> secondBargainList = secondBargainPage.getRecords();

        for(SecondBargain secondBargain : secondBargainList) {
            Choose choose = chooseService.getById(secondBargain.getOrderId());

            if(choose != null) {
                secondBargain.setOrderBatch(choose.getBatch());
                secondBargain.setOrderTime(choose.getMatchTime());
                secondBargain.setOrderName(choose.getOrderName());
                secondBargain.setProjectName(choose.getProjectName());
            }
        }

        return Result.success(new PageData(secondBargainList, new PageInfo(pageSize, currentPage, (int) secondBargainPage.getTotal())));
    }

    @Override
    public Result<Object> approve(String id) {
        return update(id, "1");
    }

    @Override
    public Result<Object> reject(String id) {
        return update(id, "0");
    }

    @Override
    public Result<Object> access(String id) {
        return update(id, "5");
    }

    @Override
    public Result<Object> received(String id) {
        return update(id, "8");
    }

    @Override
    public Result<Object> calculateCharge(String chargeType, String id) {
        //计算费用
        SecondBargain secondBargain = getById(id);
        secondBargain.setCharge(ChargeCalculateUtils.calculate(chargeType, secondBargain.getAmount()));
        secondBargain.setStatus("7"); //生成服务费用
        secondBargain.setChargeType(chargeType);

        if (updateById(secondBargain)) {
            return Result.success(secondBargain);
        }

        return Result.fail();
    }

    private Result<Object> update(String id, String status) {
        try {

            SecondBargain secondBargain = new SecondBargain();
            secondBargain.setId(id);
            secondBargain.setStatus(status);

            if (updateById(secondBargain)) {
                return Result.success();
            }
            return Result.returnMsg("保存失败",1002);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.returnMsg(e.getMessage(),1003);
        }
    }
}
